package com.geek.water.controller;


import com.geek.water.constant.CodeConstant;
import com.geek.water.dto.MenuDTO;
import com.geek.water.result.Result;
import com.geek.water.security.LoginUser;
import com.geek.water.service.MenuService;
import com.geek.water.utils.JwtHelper;
import com.geek.water.constant.MessageConstant;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chenmin
 * @desc TODO
 * @date 2025/7/26
 */
@RestController
@RequestMapping("/admin/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;

    //获取菜单信息
    @GetMapping("routes")
    public Result getRoutes(/*@RequestHeader("Authorization") String authorization*/){
        //TODO 基于Security实现获取菜单信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser)authentication.getPrincipal();
        if(!ObjectUtils.isEmpty(loginUser)){
            Integer userId = Math.toIntExact(loginUser.getUser().getId());
            List<MenuDTO> list = menuService.findAll(userId.longValue());
            if(!CollectionUtils.isEmpty(list)){
                return new Result(CodeConstant.SUCCESS , MessageConstant.GET_MENU_SUCCESS , list);
            }
        }
        return new Result(CodeConstant.FAIL , MessageConstant.GET_MENU_FAIL);

        /*if(StringUtils.hasLength(authorization)){
            String token = authorization.substring(7);
            Long userId = JwtHelper.getUserId(token);
            List<MenuDTO> list = menuService.findAll(userId);
            if(!CollectionUtils.isEmpty(list)){
                return new Result(CodeConstant.SUCCESS , MessageConstant.GET_MENU_SUCCESS , list);
            }
        }
        return new Result(CodeConstant.FAIL , MessageConstant.GET_MENU_FAIL);*/
    }

}
